<?php
    session_start();
    require_once dirname(__FILE__) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_matches.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_affiliate.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';

    $title = 'Registration';


    //GET AFF_ID
    $invite_by_user_id_hash = filter_input(INPUT_GET, 'aff_id');
    if ($invite_by_user_id_hash != NULL) {
        $invite_by_user_id = api_affiliate::dehash_aff_id($invite_by_user_id_hash);
        setcookie('aff_id', $invite_by_user_id, time() + 60 * 60 * 24 * 30 * 365);
    } elseif (!empty($_COOKIE['aff_id'])) {
        $invite_by_user_id = $_COOKIE['aff_id'];
    } else {
        $invite_by_user_id = -1;
    }

    //INITIALIZE VARIABLES
    $first_name = "";
    $last_name = "";
    $phone_number = "";
    $email = "";
    $username = "";
    $password = "";
    $password_confirm = "";
    $referral_code = "";
    $captcha = "";

    $success_info = "";
    $error_show = "";


    if (!empty($_POST)) {
        if (!empty($_POST['txt_first_name'])) {
            $first_name = $_POST['txt_first_name'];
        }
        if (!empty($_POST['txt_last_name'])) {
            $last_name = $_POST['txt_last_name'];
        }
        if (!empty($_POST['txt_phone_number'])) {
            $phone_number = $_POST['txt_phone_number'];
        }
        if (!empty($_POST['txt_email'])) {
            $email = $_POST['txt_email'];
        }
        if (!empty($_POST['txt_username'])) {
            $username = $_POST['txt_username'];
        }
        if (!empty($_POST['txt_password'])) {
            $password = $_POST['txt_password'];
        }
        if (!empty($_POST['txt_password_confirm'])) {
            $password_confirm = $_POST['txt_password_confirm'];
        }
        if (!empty($_POST['txt_captcha'])) {
            $captcha = $_POST['txt_captcha'];
        }

        $txt_referral_code = filter_input(INPUT_POST, 'txt_referral_code');
        if (!empty($txt_referral_code)) {
            $referral_id = api_affiliate::dehash_aff_id($txt_referral_code);
            if (empty($referral_id)) {
                ?>
                <script type="text/javascript">
                    alert('Wrong referral code!');
                    window.location = "<?php echo $_SERVER['PHP_SELF']; ?>";
                </script>
                <?php
                goto endscript;
            } else {
                $invite_by_user_id = $referral_id;
                setcookie("aff_id", $invite_by_user_id, time() + 60 * 60 * 24 * 30 * 365);
            }
        } else {
            $invite_by_user_id = filter_input(INPUT_POST, 'txt_aff_id');
        }

        $error_show = api_security::validate_register($first_name, $last_name,
            $phone_number, $email, $username, $password, $password_confirm, $captcha);
        if (empty($error_show)) {
            $activate_code = api_security::register($username, $email, $password, $first_name, $last_name, $phone_number, $invite_by_user_id);
            if (api_general::get_option("email_activation") === "off") {
                api_security::activate($username, $activate_code);
                $success_info = "Congratulations, you are now a member.";
            } else if (!empty($activate_code)) {
                $email_sent_to   = $email;
                $url_to_activate = LINK_ROOT . "/verify_account.php?username=$username&activate_code=$activate_code";
                $message         = "Recently, you have registered the username: $username. Please go here to activate you account: " . $url_to_activate;
                mail($email_sent_to, "Confirm Register | Betting Websites", $message);
                $success_info = "Congratulations, Register Successfully, Please go to your email $email to active your account.";
            }
            $first_name       = "";
            $last_name        = "";
            $phone_number     = "";
            $email            = "";
            $username         = "";
            $password         = "";
            $password_confirm = "";
            $captcha          = "";
        }
    }

    endscript:
?>

<!DOCTYPE html>
<!-- IE SECTION -->

<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js"> <!--<![endif]-->

                     <!-- END IE SECTION -->
<html>
<head>
    <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php' ?>
</head>
<body>
<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php' ?>

<!-- Title -->
<div class="section section-black">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="calltoaction-wrapper">
                    <h3>Register</h3>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End Title -->

<!-- List Matches -->
<div class="section">
    <div class="container">
        <div class="row">

            <div class="col-md-offset-3 col-sm-6">
                <?php
                    show_message($success_info, $error_show, 0, 0);
                ?>
                <div class="basic-login">
                    <form role="form" method="POST" action="?">
                        <input type="hidden" name="txt_aff_id" value="<?php echo $invite_by_user_id; ?>">

                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-chevron-right"></i> <b>First Name
                                    <span style="color: red">*</span></b></label>
                            <input class="form-control" name="txt_first_name" id="login-username" value="<?php echo $first_name; ?>" type="text" placeholder="">
                        </div>
                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-chevron-right"></i> <b>Last Name
                                    <span style="color: red">*</span></b></label>
                            <input class="form-control" name="txt_last_name" id="login-username" value="<?php echo $last_name; ?>" type="text" placeholder="">
                        </div>
                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-envelope"></i> <b>Email
                                    <span style="color: red">*</span></b></label>
                            <?php
                            $register_email = filter_input(INPUT_GET, "register_email");
                            $disabled_email = "";
                            if (!empty($register_email)) {
                                $email = $register_email;
                                $disabled_email = "disabled";
                            ?>
                            <input name="txt_email" value="<?php echo $email; ?>" type="hidden">
                            <?php
                            }
                            ?>
                            <input class="form-control" name="txt_email" id="login-username" value="<?php echo $email; ?>" type="text" placeholder="" <?php echo $disabled_email; ?>>
                        </div>
                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-earphone"></i> <b>Phone Number
                                    <span style="color: red">*</span></b></label>
                            <input class="form-control" name="txt_phone_number" id="login-username" value="<?php echo $phone_number; ?>" type="text" placeholder="">
                        </div>
                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-user"></i> <b>Username
                                    <span style="color: red">*</span></b></label>
                            <input class="form-control" name="txt_username" id="login-username" value="<?php echo $username; ?>" type="text" placeholder="">
                        </div>
                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-lock"></i> <b>Password
                                    <span style="color: red">*</span></b></label>
                            <input class="form-control" name="txt_password" id="login-password" value="<?php echo $password; ?>" type="password" placeholder="">
                        </div>
                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-lock"></i> <b>Re-enter Password
                                    <span style="color: red">*</span></b></label>
                            <input class="form-control" name="txt_password_confirm" id="login-password" value="<?php echo $password_confirm; ?>" type="password" placeholder="">
                        </div>
                        <div class="form-group">
                            <label><i class="glyphicon glyphicon-send"></i> <b>Refferal Code</b></label>
                            <input class="form-control" name="txt_referral_code" id="txt_referral_code" value="<?php echo $referral_code; ?>" type="text" placeholder="">
                        </div>
                        <div class="form-group form-inline">
                            <label><i class="glyphicon glyphicon-cog"></i> <b>Enter the captcha code
                                    <span style="color: red">*</span></b></label>
                            <input type="text" name="txt_captcha" id="captcha" maxlength="6" size="6" class="form-control">
                            <img src="<?php echo LINK_ROOT . DIR_INCLUDE; ?>captcha_code.php"/>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn pull-right">Register</button>
                            <div class="clearfix"></div>
                        </div>
                    </form>
                </div>
            </div>
            <!--					<div class="col-sm-7 social-login">
                                    <p>Or login with your Facebook or Twitter</p>
                                    <div class="social-login-buttons">
                                        <a href="#" class="btn-facebook-login">Login with Facebook</a>
                                        <a href="#" class="btn-twitter-login">Login with Twitter</a>
                                    </div>
                                    <div class="clearfix"></div>
                                    <div class="not-member">
                                        <p>Not a member? <a href="page-register.html">Register here</a></p>
                                    </div>
                                </div>-->
        </div>
    </div>
</div>
<!-- End List Matches -->

<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php' ?>
</body>
</html>